﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;

namespace PGBT
{
    public partial class FormNhapHang : Form
    {
        private HangHoa dbHH = null;
        private GiaoDich dbGG = null;
        private int madonhang = -1;

        private DataSet donhangchuahoantat = null;
        public FormNhapHang()
        {
            InitializeComponent();
        }

        private void FormNhapHang_Load(object sender, EventArgs e)
        {
            dbHH = new HangHoa();
            dbGG = new GiaoDich();

            donhangchuahoantat = dbGG.DanhSachDonHangChuaHoanTat();
            cbMaDonHang.DataSource = donhangchuahoantat.Tables[0];
            cbMaDonHang.DisplayMember = "madathang";
        }

        private void cbMaDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtIdNcc.Text = donhangchuahoantat.Tables[0].Rows[cbMaDonHang.SelectedIndex].ItemArray[1].ToString();
            txtTenNCC.Text = donhangchuahoantat.Tables[0].Rows[cbMaDonHang.SelectedIndex].ItemArray[2].ToString();

            madonhang = Convert.ToInt32(donhangchuahoantat.Tables[0].Rows[cbMaDonHang.SelectedIndex].ItemArray[0].ToString());
            dgvHangHoa.DataSource = dbGG.ChiTietDonHang(madonhang).Tables[0];
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string error = "";
            int manhaphang = -1;

            dbGG.BeginTransaction();
            
            try
            {
                manhaphang = dbGG.NhapHang(ref error, madonhang, System.DateTime.Now);

                if(manhaphang == -1)
                    throw new Exception(error);

                for (int i = 0; i < dgvHangHoa.Rows.Count - 1; i++)
                {
                    int mahang = Convert.ToInt32(dgvHangHoa.Rows[i].Cells["mahang"].Value.ToString());
                    float dongia = (float)Convert.ToDouble(dgvHangHoa.Rows[i].Cells["dongia"].Value.ToString());
                    DateTime handung = Convert.ToDateTime(dgvHangHoa.Rows[i].Cells["handung"].Value.ToString());

                    if(!dbGG.ThemChiTietNhapHang(ref error, manhaphang, mahang, dongia, handung))
                        throw new Exception(error);


                    int soluong = Convert.ToInt32(dgvHangHoa.Rows[i].Cells["soluong"].Value.ToString());
                    if(!dbGG.ThemChiTietHangHoa(ref error, manhaphang, mahang, soluong, dongia, handung))
                        throw new Exception(error);

                    if(!dbGG.CapNhatDonGia(ref error, mahang, dongia))
                        throw new Exception(error);
                }

                dbGG.CommitTransaction();
                MessageBox.Show("Nhập hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch(Exception ex)
            {
                dbGG.RollbackTransaction();
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvHangHoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvHangHoa.CurrentCell.RowIndex;
            txtTenHang.Text = dgvHangHoa.Rows[index].Cells["tenhang"].Value.ToString();

            if (dgvHangHoa.Rows[index].Cells["dongia"].Value != null)
                txtDonGia.Text = dgvHangHoa.Rows[index].Cells["dongia"].Value.ToString();
            else
                txtDonGia.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int index = dgvHangHoa.CurrentCell.RowIndex;
            dgvHangHoa.Rows[index].Cells["dongia"].Value = Convert.ToInt32(txtDonGia.Text);
            dgvHangHoa.Rows[index].Cells["handung"].Value = Convert.ToDateTime(txtHanDung.Text);
        }
    }
}
